// pages/pageview_contents/pageview_contents.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    introduction: {},
    task_id: null,
    kmapping_id: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this;
    console.log(options);
    that.setData({
      task_id: options.task_id,
      kmapping_id: options.kmapping_id
    })
    that.getTaskDetail();
  },
  getTaskDetail:function(){
  var that = this;
  wx.request({
    url: app.globalData.URL + '/kmapping/WebAppAjax/getTaskDetail&token=' + wx.getStorageSync('token'),
    dataType: 'json',
    method: 'GET',
    data: {
      task_id: that.data.task_id,
      kmapping_id: that.data.kmapping_id
    },
    success: function (res) {
      var _res = res.data;
      if (_res.error_code == 0) {
        var _introduction = _res.data;

        _introduction.webapp_kmapping_learn = _introduction.webapp_kmapping_learn.replace('?', '&');
        _introduction.webapp_diagnostic_volume = _introduction.webapp_diagnostic_volume.replace('?', '&');
        that.setData({
          'introduction': _introduction
        })
      }
    }
  })
  },
  startLearnHandle: function() {
    var that = this;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/addTaskLog?' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        resource_id: 0,
        type: 2,
        task_id: that.data.introduction.task_id,
        structure_id: 0,
        kmapping_id: that.data.introduction.kmapping_id
      },
      success: function(res) {
        var _res = res.data;
        var taskId=_res.task_id;
        if (_res.error_code == 0) {
          if(that.data.task_id==0){
            that.setData({
              task_id:taskId,
              'introduction.task_id': taskId
            })
          }else{
            that.setData({
              task_id: that.data.task_id,
              'introduction.task_id': that.data.task_id
            })
          }
        }
        wx.navigateTo({
          url: '/pages/webView/webView?url=' + that.data.introduction.webapp_kmapping_learn + '&resouce_id=0&type=2&task_id=' + that.data.task_id + '&structure_id=0&kmapping_id=' + that.data.kmapping_id
        })
      }
    })
  },

  collectHandle: function(e) {
    var that = this,
      introduction = that.data.introduction;
    that.getCollected();
  },

  uncollectHandle: function(e) {
    var that = this,
      introduction = that.data.introduction;
    that.getunCollected();
  },

  getCollected: function() {
    var that = this,
      introduction = that.data.introduction;
    wx.request({
      url: app.globalData.URL + '/index.php?app=interface&mod=Collection&act=addCollection&' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        type: 213,
        id: introduction.kmapping_id
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          introduction.is_collection = 1;
          that.setData({
            introduction: introduction
          })
          that.updataCollectStatus(introduction.kmapping_id, 1);
          wx.showToast({
            title: '收藏成功',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: _res.error_msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  getunCollected: function() {
    var that = this,
      introduction = that.data.introduction;
    wx.request({
      url: app.globalData.URL + '/index.php?app=interface&mod=Collection&act=deleteCollection&' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        type: 213,
        id: introduction.kmapping_id
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          introduction.is_collection = 0;
          that.setData({
            introduction: introduction
          })
          that.updataCollectStatus(introduction.kmapping_id,0);
          wx.showToast({
            title: '取消收藏成功',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: _res.error_msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 收藏或取消收藏操作更新之后更新首页相关图谱的收藏状态
  */
  updataCollectStatus: function (kmapping_id,status){
    var pages = getCurrentPages();
    var index = null;
    for (let i = 0; i < pages.length; i++) {
      if (pages[i].route == 'pages/index/index') {
        index = pages[i];
      }
    }
    var tupu = index.data.tupu,
      commend = index.data.commend;
    for (let i = 0; i < tupu.length; i ++){
      if (tupu[i].kmapping_id == kmapping_id){
        tupu[i].is_collection = status;
      }
    }
    for (let i = 0; i < commend.length; i++) {
      if (commend[i].kmapping_id == kmapping_id) {
        commend[i].is_collection = status;
      }
    }
    index.setData({
      tupu: tupu,
      commend: commend
    })
  },

  navToPageForWebView: function() {
    var that = this;
    if (that.data.task_id == 0 || that.data.task_id == null){
      wx.request({
        url: app.globalData.URL + '/kmapping/WebAppAjax/addTaskLog?' + app.globalData.URLBOOL,
        dataType: 'json',
        method: 'GET',
        data: {
          resource_id: 0,
          type: 2,
          task_id: that.data.introduction.task_id,
          structure_id: 0,
          kmapping_id: that.data.introduction.kmapping_id
        },
        success: function (res) {
          var _res = res.data;
          var taskId = _res.task_id;
          if (_res.error_code == 0) {
            if (that.data.task_id == 0) {
              that.setData({
                task_id: taskId,
                'introduction.task_id': taskId
              })
            } else {
              that.setData({
                task_id: that.data.task_id,
                'introduction.task_id': that.data.task_id
              })
            }
          }
          wx.navigateTo({
            url: '/pages/webView/webView?url=' + that.data.introduction.webapp_kmapping_learn + '&resouce_id=0&type=2&task_id=' + that.data.task_id + '&structure_id=0&kmapping_id=' + that.data.kmapping_id
          })
          // wx.navigateTo({
          //   url: '/pages/webView/webView?url=' + _this.data.introduction.webapp_kmapping_learn + '&task_id=' + _this.data.task_id,
          // })
        }
      })
    }
    else{
      wx.navigateTo({
        url: '/pages/webView/webView?url=' + that.data.introduction.webapp_kmapping_learn + '&resouce_id=0&type=2&task_id=' + that.data.task_id + '&structure_id=0&kmapping_id=' + that.data.kmapping_id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var that=this;
    that.getTaskDetail();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})